package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.BankItem;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.WithDrawBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 提现界面
 */
public class WithDrawActivity extends MultiStatusActivity {

    @BindView(R.id.withdraw)
    Button withdraw;
    @BindView(R.id.kyTv)
    TextView kyTv;
    @BindView(R.id.inputMoneyEt)
    EditText inputMoneyEt;
    @BindView(R.id.chooseICCardTv)
    TextView chooseICCardTv;
    @BindView(R.id.chooseCardFl)
    FrameLayout chooseCardFl;
    @BindView(R.id.identifyNumber)
    TextView identifyNumber;
    @BindView(R.id.withdrawpersent)
    TextView withdrawpersent;
    private String money;
    private BankItem defaultBankItem = null;
    private String poundage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        money = getIntent().getExtras().getString("money");
        poundage = getIntent().getExtras().getString("poundage");
        ButterKnife.bind(this);
        kyTv.setText("可用¥" + money);
        withdrawpersent.setText("提现手续费" + poundage + "%");
        initTitleBar();
    }

    private void initTitleBar() {
        initTitleBarView(titlebar, "提现");
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.card);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加新卡

                ActivityUtils.startActivity(WithDrawActivity.this, AddNewICCardActivity.class);
            }
        });
        titlebar.setRightView(imageView);
        ((RelativeLayout.LayoutParams) imageView.getLayoutParams()).rightMargin = SizeUtils.dp2px(16);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_with_draw;
    }

    @OnClick({R.id.chooseCardFl, R.id.withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chooseCardFl:
                loadICCardList();
                break;
            case R.id.withdraw:
                if (TextUtils.isEmpty(inputMoneyEt.getText().toString())) {
                    ToastUtils.showShort("提现金额不能为空");
                    return;
                }

                float money = Float.parseFloat(inputMoneyEt.getText().toString());


                if (defaultBankItem == null) {
                    ToastUtils.showShort("请先选中银行卡");
                } else if (money <= 0) {
                    ToastUtils.showShort("提现金额不能小于等于0");

                } else {
                    widthDraw();
                }


                break;
        }
    }

    private void widthDraw() {
        prompDialog.showLoading("请等待");
        RetrofitManager.create().cash(SPUtils.getInstance().getString("uid"), inputMoneyEt.getText().toString(), defaultBankItem.getUser_name(), defaultBankItem.getBank_name(), defaultBankItem.getBankcard_number())
                .compose(this.<CommonResonseBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            //提现成功
                            prompDialog.showSuccess("提现成功,请注意查收");
                            finish();
                        }
                    }
                });

    }

    private void loadICCardList() {
        prompDialog.showLoading("请等待");
        RetrofitManager.create().bankcard_list(SPUtils.getInstance().getString("uid"))
                .compose(this.<WithDrawBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<WithDrawBean>(prompDialog) {
                    @Override
                    public void onSuccess(WithDrawBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            if (value.getInfo() == null || value.getInfo().size() == 0) {
                                ToastUtils.showShort("请先添加银行卡");
                                ActivityUtils.startActivity(WithDrawActivity.this, AddNewICCardActivity.class);

                            } else {

                                showICCardList(value.getInfo());

                            }
                        }
                    }
                });
    }

    private void showICCardList(final List<BankItem> info) {

        BaseAdapter adapter = new ArrayAdapter<BankItem>(this, android.R.layout.simple_list_item_1, info) {

        };

        TextView view = new TextView(this);
        view.setText("请选择银行卡");
        view.setTextSize(16);
        view.setTextColor(getResources().getColor(R.color.textColorPrimary));
        int dimension = (int) getResources().getDimension(R.dimen.layoutMargin16dp);
        view.setPadding(dimension, dimension, dimension, dimension);
        DialogPlus dialog = DialogPlus.newDialog(this)
                .setAdapter(adapter)
                .setGravity(Gravity.CENTER)
                .setHeader(view)
                .setContentWidth(ViewGroup.LayoutParams.WRAP_CONTENT)  // or any custom width ie: 300
                .setContentHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {

                        defaultBankItem = info.get(position);
                        chooseICCardTv.setText(defaultBankItem.getBank_name());
                        dialog.dismiss();
                    }
                })
                .setExpanded(false)  // This will enable the expand feature, (similar to android L share dialog)
                .create();
        dialog.show();
    }
}
